﻿/*
 * @Autor: PK
 * @Date: 2021/12/27 10:53:SS
 */

using System;
using System.Drawing;
using System.Windows.Forms;

namespace ShkSimulation.desktop.component.tools {

	public delegate void CheckBoxClickedHandler(bool state);

	public class DataGridViewCheckBoxHeaderCellEventArgs : EventArgs {

		bool _bChecked;

		public DataGridViewCheckBoxHeaderCellEventArgs(bool bChecked) {
			_bChecked = bChecked;
		}

		public bool Checked {
			get { return _bChecked; }
		}

	}

	public class DatagridViewCheckBoxHeaderCell : DataGridViewColumnHeaderCell {

		Point checkBoxLocation;
		Size checkBoxSize;
		bool _checked;
		Point _cellLocation;

		System.Windows.Forms.VisualStyles.CheckBoxState _cbState =
			System.Windows.Forms.VisualStyles.CheckBoxState.UncheckedNormal;

		public event CheckBoxClickedHandler OnCheckBoxClicked;

		public override object Clone() {
			DatagridViewCheckBoxHeaderCell obj = (DatagridViewCheckBoxHeaderCell)base.Clone();
			obj.OnCheckBoxClicked += OnCheckBoxClicked;
			return obj;
		}

		protected override void Paint(Graphics graphics,
			Rectangle clipBounds,
			Rectangle cellBounds,
			int rowIndex,
			DataGridViewElementStates dataGridViewElementState,
			object value,
			object formattedValue,
			string errorText,
			DataGridViewCellStyle cellStyle,
			DataGridViewAdvancedBorderStyle advancedBorderStyle,
			DataGridViewPaintParts paintParts) {
			base.Paint(graphics, clipBounds, cellBounds, rowIndex,
				dataGridViewElementState, value,
				formattedValue, errorText, cellStyle,
				advancedBorderStyle, paintParts);
			Point p = new Point();
			Size s = CheckBoxRenderer.GetGlyphSize(graphics,
				System.Windows.Forms.VisualStyles.CheckBoxState.UncheckedNormal);
			p.X = cellBounds.Location.X +
				(cellBounds.Width / 2) - (s.Width / 2) - 1;
			p.Y = cellBounds.Location.Y +
				(cellBounds.Height / 2) - (s.Height / 2);
			_cellLocation = cellBounds.Location;
			checkBoxLocation = p;
			checkBoxSize = s;
			if (_checked)
				_cbState = System.Windows.Forms.VisualStyles.CheckBoxState.CheckedNormal;
			else
				_cbState = System.Windows.Forms.VisualStyles.CheckBoxState.UncheckedNormal;
			CheckBoxRenderer.DrawCheckBox
				(graphics, checkBoxLocation, _cbState);
		}

		protected override void OnMouseClick(DataGridViewCellMouseEventArgs e) {
			Point p = new Point(e.X + _cellLocation.X, e.Y + _cellLocation.Y);
			if (p.X >= checkBoxLocation.X && p.X <=
			                              checkBoxLocation.X + checkBoxSize.Width
			                              && p.Y >= checkBoxLocation.Y && p.Y <=
			                              checkBoxLocation.Y + checkBoxSize.Height) {
				_checked = !_checked;
				if (OnCheckBoxClicked != null) {
					OnCheckBoxClicked(_checked);
					this.DataGridView.InvalidateCell(this);
				}

			}
			base.OnMouseClick(e);
		}

	}

}